import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/common/client/common_http_client.dart';
import 'package:dk_flutter_module_sale/common/user_track/user_track.dart';
import 'package:dk_flutter_module_sale/phoenix/api/revision_main_api.dart';
import 'package:dk_flutter_module_sale/portal/api/portal_api.dart';
import 'package:dk_flutter_module_sale/r.dart';
import 'package:dk_flutter_module_sale/sale/api/sale_in_api.dart';
import 'package:dk_flutter_module_sale/sale/api/sale_out_api.dart';


abstract class CommonPage extends BasePage<CommonPageBloc> {
  @override
  CommonPageBloc getPageBloc() {
    return CommonPageBloc();
  }
}

abstract class CommonPageWithTrack extends BasePage<CommonPageBloc> {
  @override
  CommonPageBloc getPageBloc() {
    trackStatistics();
    return CommonPageBloc();
  }

  void trackStatistics();
}


class CommonPageBloc extends PageBloc
    with HttpBloc, ContainerBloc, EventBusBloc {
  @override
  BaseHttpClient getHttpClient() {
    return CommonHttpClient.instance;
  }

  @override
  LogicPageConfig getLogicPageConfig(String path) {
    if (stringBlank(path)) {
      return null;
    }
    if (path.startsWith(PortalApi.portalBaseUrl)) {
      return PortalConfig.portalLogicPageConfig;
    }
    if (path.startsWith(RevisionMainApi.revisionMainBaseUrl)) {
      return PortalConfig.portalLogicPageConfig;
    }
    if (path.startsWith(SaleInApi.saleInBaseUrl) ||
        path.startsWith(SaleOutApi.saleOutBaseUrl)) {
      return SaleConfig.saleLogicPageConfig;
    }
    return SaleConfig.saleLogicPageConfig;
  }

  @override
  BuildContext getBuildContext() {
    return context;
  }
}

abstract class CommonScaffoldPage extends BaseScaffoldPage<CommonPageBloc> {
  @override
  CommonPageBloc getPageBloc() {
    return CommonPageBloc();
  }
}

class SaleConfig {
  static LogicPageConfig saleLogicPageConfig = LogicPageConfig(
    empty: emptyPageParam,
    error: errorPageParam,
    retry: retryPageParam,
  );

  static LogicParam emptyPageParam = LogicParam.logicEmpty(
    imageUrl: R.imagesSaleoutPageEmptyPng,
    imageWidth: 180.0,
    imageHeight: 180.0,
    tip: "暂无数据",
    tipStyle: TextStyle(
      fontSize: 16,
      color: Color(0xffA3A3A3),
    ),
  );

  static LogicParam errorPageParam = LogicParam.logicError(
    imageUrl: R.imagesSaleoutPageEmptyPng,
    imageWidth: 180.0,
    imageHeight: 180.0,
    tip: "加载错误",
    tipStyle: TextStyle(
      fontSize: 16,
      color: Color(0xffA3A3A3),
    ),
    btnContent: "重试",
  );

  static LogicParam retryPageParam = LogicParam.logicRetry(
    imageUrl: R.imagesSaleoutPageEmptyPng,
    imageWidth: 180.0,
    imageHeight: 180.0,
    tip: "网络错误",
    tipStyle: TextStyle(
      fontSize: 16,
      color: Color(0xffA3A3A3),
    ),
    btnContent: "重试",
  );
}

class PortalConfig {
  static LogicPageConfig portalLogicPageConfig = LogicPageConfig(
    empty: emptyPageParam,
    error: errorPageParam,
    retry: retryPageParam,
  );

  static LogicParam emptyPageParam = LogicParam.logicEmpty(
    imageUrl: R.imagesPortalPageEmptyPng,
    imageWidth: 180.0,
    imageHeight: 180.0,
    tip: "暂无数据",
    tipStyle: TextStyle(
      fontSize: 16,
      color: Color(0xffA3A3A3),
    ),
  );
  static LogicParam errorPageParam = LogicParam.logicEmpty(
    imageUrl: R.imagesPortalPageErrorPng,
    imageWidth: 180.0,
    imageHeight: 180.0,
    tip: "加载错误",
    tipStyle: TextStyle(
      fontSize: 16,
      color: Color(0xffA3A3A3),
    ),
    btnContent: "重试",
  );
  static LogicParam retryPageParam = LogicParam.logicEmpty(
    imageUrl: R.imagesPortalPageErrorPng,
    imageWidth: 180.0,
    imageHeight: 180.0,
    tip: "网络错误",
    tipStyle: TextStyle(
      fontSize: 16,
      color: Color(0xffA3A3A3),
    ),
    btnContent: "重试",
  );

  static LogicParam smallEmptyPageParam = LogicParam.logicEmpty(
    imageUrl: R.imagesPortalPageEmptyPng,
    imageWidth: 100.0,
    imageHeight: 100.0,
    tip: "暂无数据",
    tipStyle: TextStyle(
      fontSize: 14,
      color: Color(0xFF666666),
    ),
  );
}
